package STUDENT.TRANDAODAIVI.THUVIEN.VIEWS;
import java.util.InputMismatchException;
import java.util.Scanner;

import STUDENT.TRANDAODAIVI.THUVIEN.CONTROLLERS.Controllers3;
import STUDENT.TRANDAODAIVI.THUVIEN.CONTROLLERS.Controllers2;
import STUDENT.TRANDAODAIVI.THUVIEN.IDENTITY.UserInfo;
import STUDENT.TRANDAODAIVI.THUVIEN.CONTROLLERS.Controllers1;
import STUDENT.TRANDAODAIVI.THUVIEN.IDENTITY.BookInfo;
import STUDENT.TRANDAODAIVI.THUVIEN.IDENTITY.BookManager;
import java.io.*;
public class Views1 {
	static Controllers1 Do1 =new Controllers1();
	static Controllers2 Do2 =new Controllers2();
	static Controllers3 Do3= new Controllers3();

	public static void showMenu(){
		System.out.println("-------Library------");
		System.out.println("");
		System.out.println("1.Add Member");
		System.out.println("2.Show Member");
		System.out.println("3.Add Book");
		System.out.println("4.Show Book");
		System.out.println("5.Add Borrower");
		System.out.println("6.Show Borrower");
		System.out.println("7.Book Ranking");
		System.out.println("");
		System.out.print("Input your option: ");
	}
	
	public static void addBook() {
		Scanner sc=new Scanner(System.in);
		BookInfo book;
		
		System.out.println("-----------------------");
		
		System.out.println("Chuong trinh nhap sach muon them vao");
		System.out.print("Nhap so luong sach muon them vao: ");
		int temp1=sc.nextInt();
		
	
		for (int i=0;i<temp1;i++){
			
			
			book=new BookInfo();
			
			if(Do1.getSize()==0){
				book.setId(1);
				} else {book.setId(Do1.getSize()+1);
				}
			System.out.println("Ma sach              : "+book.getId());	
			
			sc=new Scanner(System.in);
			System.out.print("Nhap ten sach        : ");
			book.setName(sc.nextLine());
			System.out.print("Nhap ten tac gia     : ");
			book.setAuthor(sc.nextLine());
			System.out.print("Nhap ten chuyen nganh: ");
			book.setSpeciality(sc.nextLine());
			System.out.print("Nhap ngay xuat ban   : ");
			book.setReleaseDate(sc.nextInt());
			sc=new Scanner(System.in);
			Do1.addBooks(book);
			
		}
	}
	public static void showBook(){
		System.out.println("-----------------------");
		Do1.showBooks();
	}
	public static void addMember() {
		Scanner sc=new Scanner(System.in);
		UserInfo user;
		System.out.println("-----------------------");
		System.out.println("Chuong trinh nhap danh sach ban doc");
		System.out.print("Nhap so luong thanh vien moi muon them vao: ");
		int temp1=sc.nextInt();
		for (int i=0;i<temp1;i++){
			user=new UserInfo();
			if(Do2.getSize()==0){
				user.setId(1);
				} else {user.setId(Do2.getSize()+1);
				}
			System.out.println("Nhap ma thanh vien     : "+user.getId());
			
			sc=new Scanner(System.in);
			System.out.print("Nhap ten thanh vien    : ");
			user.setName(sc.nextLine());
			System.out.print("Nhap dia chi           : ");
			user.setAddress(sc.nextLine());
			System.out.print("Nhap so dien thoai     : ");
			user.setTelephone(sc.nextInt());
			sc=new Scanner(System.in);
			System.out.print("Nhap loai thanh vien   : ");
			user.setType(sc.nextLine());
			Do2.addMember(user);
		}
	}
	public static void showMember(){
		System.out.println("-----------------------");
		Do2.showMember();
	}
	
	public static void addBorrower(){
		Scanner sc=new Scanner(System.in);
		BookManager bookmanager;

		System.out.println("-----------------------");
		System.out.println("Nhap danh sach hoat dong hom nay");
		System.out.print("Nhap danh sach so hoat dong: ");
		int temp1=sc.nextInt();
		
		for (int i=0; i<temp1;i++){
			bookmanager=new BookManager();
			
			int f=0;
			do{
			f=0;
			sc=new Scanner(System.in);
			System.out.print("Nhap ID nguoi muon : ");
			bookmanager.setUserid(sc.nextInt());
			
			
			f=Do2.checkMember(bookmanager.getUserid());
			
			
			System.out.print("Nhap ID sach       : ");
			bookmanager.setBookid(sc.nextInt());
			sc=new Scanner(System.in);
			System.out.print("Nhap ten sach      : ");
			bookmanager.setBookname(sc.nextLine());
			
			if(f!=-1){
			f=Do1.checkBook(bookmanager.getBookid(),bookmanager.getBookname());
			}
			if(f!=-1&&Do1.checkBook(bookmanager.getBookid(),bookmanager.getBookname())==1){
				f=Do3.checkDuplicatedIdBook(bookmanager.getBookid());
			}
			
			if(f==-1){
				System.out.println("");
				System.out.println("Vui long nhap lai thong tin");
				System.out.println("");
			}
			}
			while(f==-1);
			
			Do3.addBorrower(bookmanager);
			}

	}
	public static void showBorrower(){
		System.out.println("-----------------------");
		Do3.showBorrower();
	}
	
	public static void rankBook(){
		Do3.rankBook();
	}
	public static void main(String[] args) {
		
		int z=0;
		do{
			Views1.showMenu();
			int k=0;
			int temp=0;
			do{
				
			k=0;
			try{	
			Scanner sc=new Scanner(System.in);
			temp=sc.nextInt();
			}
			catch(InputMismatchException exc){
				k=-1;
				System.out.print("Please input your option correctly: ");
				
			}
			//Khong biet tai sao k==0&temp<1&&temp>6 thi khong hoat dong
			if(k==0){
			if(temp!=1&&temp!=2&&temp!=3&&temp!=4&&temp!=5&&temp!=6&&temp!=7){
				System.out.print("Please input your option correctly: ");
				k=-1;
			}
			}
			}
			while(k==-1);
			
			
			
			int option=temp;
			switch(option){
			case 1: Views1.addMember();break;
			case 2: Views1.showMember();break;
			case 3: Views1.addBook();break;
			case 4: Views1.showBook();break;
			case 5: Views1.addBorrower();break;
			case 6: Views1.showBorrower();break;
			case 7: Views1.rankBook();break;
			}
			System.out.println("");
		}
		while (z==0);
		
	}
}

